import java.util.Random;


public class BruteForce {
	/**
	 * Smart Exhaustive search method
	 * Called By Game
	 * Uses Randomized search algorithm
	 * 
	 * @author Casey Huckins
	 * @modified Larry Savago
	 * @date 4/13/2012
	 */
	
	Board board;
	private int counter;
	
	public int search(Board mboard, Ball ball){
		board = mboard;
		int direction = 0;
		counter = 0;
		int row = 0;
		int col = 0;
		boolean found = false;
		Square searchSquare;
		
		
		 int arraySize = board.ROWS * board.COLS;
		 
		 
		 Square[] x = new Square[arraySize];
		 Square[] y = new Square[arraySize];
		 
		 
		 // build 1-d array of possible searches.. 
		 int u = 0;
		 for (int i = 0; i<board.ROWS; i++) {
			 for (int t = 0; t<board.COLS; t++){
				x[u] = board.getSquare(i, t);
				y[u] = board.getSquare(i, t);
				u++;
			 }
		 }
		 // and then search through them until you found the right one.
		 // after each search remove that search from the possible ones and reduce your random to minus one
		 Random randomGenerator =new Random(); 
		 for (int r =0; r< x.length; r++) {
			 int randomInt = randomGenerator.nextInt(arraySize); //gets a random number between 0-8 then gets smaller 
			 searchSquare = x[randomInt];
			 //System.out.println("" + randomInt);
			 //System.out.println("" + ball.search(searchSquare));
			 counter++;
			 if (ball.search(searchSquare)){
				 break;
			 }
			 
			 // move the searched square to the end of the array [(X to Y) and then copy it back to X]
			 System.arraycopy(x, randomInt + 1, y, randomInt, (x.length-1)-randomInt);
			 System.arraycopy(y, 0, x, 0, x.length-1); //x = y;
			 arraySize--;  // decrement arraySize so we don't check that square again
		 }
		 	
			System.out.println("Brute Force Count = " + counter);
			

		return counter;
	}
	

}
